// @ts-ignore
const Cartesian3 = Cesium.Cartesian3;
// @ts-ignore
const Color = Cesium.Color;

/**
 *
 * @param {*} hydata
 * @param {*} triangData
 * @returns {{
 *   vectors: Float64Array
 *   index: number[]
 *   sts: number[]
 * }}
 * @deprecated
 */
const resetVector = (hydata, triangData) => {
  var coordMap = {};
  var coordArrayByOrder = new Float64Array(3 * 18217).fill(0);
  var clorArray = new Float64Array(18217).fill(0);
  var colors = [];
  for (var i = 0; i < hydata.length; i++) {
    coordMap[hydata[i].Longitude + '_' + hydata[i].Latitude] = parseFloat(hydata[i].eastward);
  }
  var vectorIndex = [];
  var height = 1120;
  for (var j = 0; j < triangData.length; j++) {
    var temp = triangData[j];
    vectorIndex.push(parseInt(temp.POINTA), parseInt(temp.POINTB), parseInt(temp.POINTC));
    var tempCartesian = null;
    if (coordArrayByOrder[parseInt(temp.POINTA) * 3 + 2] == 0) {
      clorArray[parseInt(temp.POINTA)] = coordMap[temp.WKT[0] + '_' + temp.WKT[1]];
      //tempCartesian=Cesium.Cartesian3.fromDegrees(temp.WKT[0],temp.WKT[1],coordMap[temp.WKT[0]+'_'+temp.WKT[1]]);
      tempCartesian = Cartesian3.fromDegrees(temp.WKT[0], temp.WKT[1], height);
      coordArrayByOrder[parseInt(temp.POINTA) * 3] = tempCartesian.x; //temp.WKT[0];
      coordArrayByOrder[parseInt(temp.POINTA) * 3 + 1] = tempCartesian.y; //temp.WKT[1];
      coordArrayByOrder[parseInt(temp.POINTA) * 3 + 2] = tempCartesian.z; //coordMap[temp.WKT[0]+'_'+temp.WKT[1]]
    }
    if (coordArrayByOrder[parseInt(temp.POINTB) * 3 + 2] == 0) {
      clorArray[parseInt(temp.POINTB)] = coordMap[temp.WKT[2] + '_' + temp.WKT[3]];
      //tempCartesian=Cesium.Cartesian3.fromDegrees(temp.WKT[2],temp.WKT[3],coordMap[temp.WKT[2]+'_'+temp.WKT[3]]);
      tempCartesian = Cartesian3.fromDegrees(temp.WKT[2], temp.WKT[3], height);
      coordArrayByOrder[parseInt(temp.POINTB) * 3] = tempCartesian.x; //temp.WKT[2];
      coordArrayByOrder[parseInt(temp.POINTB) * 3 + 1] = tempCartesian.y; //temp.WKT[3];
      coordArrayByOrder[parseInt(temp.POINTB) * 3 + 2] = tempCartesian.z; //coordMap[temp.WKT[2]+'_'+temp.WKT[3]]
    }
    if (coordArrayByOrder[parseInt(temp.POINTC) * 3 + 2] == 0) {
      clorArray[parseInt(temp.POINTC)] = coordMap[temp.WKT[4] + '_' + temp.WKT[5]];
      //tempCartesian=Cesium.Cartesian3.fromDegrees(temp.WKT[4],temp.WKT[5],coordMap[temp.WKT[4]+'_'+temp.WKT[5]]);
      tempCartesian = Cartesian3.fromDegrees(temp.WKT[4], temp.WKT[5], height);
      coordArrayByOrder[parseInt(temp.POINTC) * 3] = tempCartesian.x; //temp.WKT[4];
      coordArrayByOrder[parseInt(temp.POINTC) * 3 + 1] = tempCartesian.y; //temp.WKT[5];
      coordArrayByOrder[parseInt(temp.POINTC) * 3 + 2] = tempCartesian.z; //coordMap[temp.WKT[4]+'_'+temp.WKT[5]]
    }
  }
  for (var n = 0; n < clorArray.length; n++) {
    colors.push(clorArray[n] * 15, 255);
  }
  return {
    vectors: coordArrayByOrder,
    index: vectorIndex,
    sts: colors
  };
};

/**
 *
 * @param {object} inputData 输入数据
 * @param {number[]} inputData.renderData
 * @param {number[]} inputData.triangleData
 * @param {string} fieldName
 * @param {any[]} colorRange
 * @returns
 */
const resetVector2 = (inputData, fieldName, colorRange) => {
  const { renderData, triangleData } = inputData;
  const coordMap = {};
  const coordArrayByOrder = new Float64Array(3 * 18217).fill(0);
  const colorArray = new Float64Array(18217).fill(0);
  // const colors = []
  const colorArr = [];
  for (let i = 0; i < renderData.length; i++) {
    coordMap[`${renderData[i]['Longitude']}_${renderData[i]['Latitude']}`] = parseFloat(
      renderData[i][fieldName]
    );
  }
  const vectorIndex = [];
  const height = 1120;
  for (let i = 0; i < triangleData.length; i++) {
    const temp = triangleData[i];
    const pA = parseInt(temp['POINTA']);
    const pB = parseInt(temp['POINTB']);
    const pC = parseInt(temp['POINTC']);
    const wkt = temp['WKT'];
    vectorIndex.push(pA, pB, pC);
    let tempCartesian = null;
    if (coordArrayByOrder[pA * 3 + 2] === 0) {
      colorArray[pA] = coordMap[`${wkt[0]}_${wkt[1]}`];
      tempCartesian = Cartesian3.fromDegrees(wkt[0], wkt[1], height);
      coordArrayByOrder[pA * 3] = tempCartesian.x;
      coordArrayByOrder[pA * 3 + 1] = tempCartesian.y;
      coordArrayByOrder[pA * 3 + 2] = tempCartesian.z;
    }
    if (coordArrayByOrder[pB * 3 + 2] === 0) {
      colorArray[pB] = coordMap[`${wkt[2]}_${wkt[3]}`];
      tempCartesian = Cartesian3.fromDegrees(wkt[2], wkt[3], height);
      coordArrayByOrder[pB * 3] = tempCartesian.x;
      coordArrayByOrder[pB * 3 + 1] = tempCartesian.y;
      coordArrayByOrder[pB * 3 + 2] = tempCartesian.z;
    }
    if (coordArrayByOrder[pC * 3 + 2] === 0) {
      colorArray[pC] = coordMap[`${wkt[4]}_${wkt[5]}`];
      tempCartesian = Cartesian3.fromDegrees(wkt[4], wkt[5], height);
      coordArrayByOrder[pC * 3] = tempCartesian.x;
      coordArrayByOrder[pC * 3 + 1] = tempCartesian.y;
      coordArrayByOrder[pC * 3 + 2] = tempCartesian.z;
    }
  }

  colorArray.sort((a, b) => a - b);
  const range = [];
  const min = colorArray[0];
  const max = colorArray[colorArray.length - 1];
  const interval = max - min;
  const pLevel = colorRange.length;
  for (let i = 0; i < pLevel; i++) {
    i === pLevel - 1 ? range.push(max + 1) : range.push(min + (interval / pLevel) * (i + 1));
  }

  const findRangeColor = (array, value) => {
    if (value < Math.min.apply(null, array)) {
      return 0;
    }
    if (value > Math.max.apply(null, array)) {
      return array.length - 1;
    }
    let index = 0;
    const length = array.length;
    for (let i = 0; i < length; i++) {
      if (array[i] > value) {
        index = i;
        break;
      }
    }
    return index;
  };

  const generateMeshColor = (rainValue, alphaDefault) => {
    let currentColor = Color.fromBytes(255, 255, 255, 120);
    if (alphaDefault === undefined) {
      alphaDefault = 0.2;
    }
    if (rainValue > 0) {
      currentColor = colorRange[findRangeColor(range, rainValue)];
    }
    return currentColor;
  };

  for (let i = 0; i < colorArray.length; i++) {
    const zFieldValue = colorArray[i];
    const tempColor = generateMeshColor(zFieldValue, 0.6);
    colorArr.push(tempColor.red, tempColor.green, tempColor.blue);
  }

  return {
    vectors: coordArrayByOrder,
    index: vectorIndex,
    sts: colorArr
  };
};

export { resetVector2 as resetVector };
